package org.zlb.netty.server.rpc;

import org.zlb.netty.dbo.RpcMessage;
import org.zlb.netty.dbo.RpcRequest;
import org.zlb.netty.dbo.RpcResponse;
import org.zlb.netty.util.ProtostuffUtil;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcMessageEncode extends MessageToByteEncoder<RpcMessage>{

    @Override
    protected void encode(ChannelHandlerContext ctx, RpcMessage msg, ByteBuf out) throws Exception {
        byte[] data = null;
        if (msg instanceof RpcRequest) {
            RpcRequest req = (RpcRequest)msg;
            data = ProtostuffUtil.serialize(req);
        } else if (msg instanceof RpcResponse) {
            RpcResponse resp = (RpcResponse)msg;
            data = ProtostuffUtil.serialize(resp);
        }
        out.writeBytes(data);
    }

}
